package com.daguchuangyi.erp_android.productlist.classifiaction;

import android.content.DialogInterface;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;

import com.daguchuangyi.erp_android.R;
import com.daguchuangyi.erp_android.productlist.widget.BaseSlideDialog;
import com.daguchuangyi.erp_android.util.ErrorHandler;
import com.jude.utils.JUtils;

import java.util.Map;

import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

/**
 * Created by lijianjian on 2018/3/10.
 */

public class ClassificationMenuDialog extends BaseSlideDialog {

    private RecyclerView mClassficationRv;
    private ClassificationTreeAdapter mClassificationTreeAdapter;
    private CompositeSubscription mCompositeSubscription = new CompositeSubscription();

    @Override
    public int getLayoutRes() {
        return R.layout.dialog_product_classification_layout;
    }

    @Override
    public void bindView(View v) {
        mClassficationRv = (RecyclerView) v.findViewById(R.id.classification_rv);
        mClassficationRv.setLayoutManager(new LinearLayoutManager(getContext()));

        mClassificationTreeAdapter = new ClassificationTreeAdapter(R.layout.item_node_layout, null);
        mClassficationRv.setAdapter(mClassificationTreeAdapter);

        v.findViewById(R.id.cancel_btn).setOnClickListener(view -> dismiss());
        v.findViewById(R.id.confirm_btn).setOnClickListener(view -> {
            Map<Integer, ClassificationModel> modelMap = mClassificationTreeAdapter.getCheckedTree();
            int size = modelMap.size();
            if (JUtils.DEBUG) {
                for (int i = 0; i < size; i++) {
                    ClassificationModel item = modelMap.get(i);
                    String log = String.format("Id : %s,title : %s,level : %d", item.getId(), item.getTitle(), item.getLevel());
                    JUtils.Log(log);
                }
            }
            ClassificationManager.getInstance().refreshClassification(modelMap);
            dismiss();
        });

        loadData();
    }

    private void loadData() {
        Subscription subscription = ClassificationManager.getInstance().loadClassificationInfo(true)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(
                        classificationModels -> {
                            mClassificationTreeAdapter.setNewData(classificationModels);
                        },
                        throwable -> {
                            ErrorHandler.handlerNetworError(throwable,"加载分类失败");
                            throwable.printStackTrace();
                        });
        mCompositeSubscription.add(subscription);
    }

    @Override
    public int getDialogStyle() {
        return R.style.LeftSlideDialog;
    }

    @Override
    public int getWidth() {
        return JUtils.dip2px(280);
    }

    @Override
    public int getHeight() {
        return WindowManager.LayoutParams.MATCH_PARENT;
    }

    @Override
    public int getWindowGravity() {
        return Gravity.LEFT;
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        mCompositeSubscription.clear();
        super.onDismiss(dialog);
    }
}
